package com.qys.livesMall.shop.service;

import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.finance.model.dto.FinancePayDTO;
import com.qys.livesMall.finance.service.FinanceBusinessService;
import com.qys.livesMall.shop.constants.OrderStatus;
import com.qys.livesMall.shop.dao.ShopOrderSettlementDao;
import com.qys.livesMall.shop.modal.dto.settlement.SettlementHandleContext;
import com.qys.livesMall.shop.modal.dto.settlement.SuperiorInfo;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.entity.ShopOrderSettlementInfo;
import com.qys.livesMall.shop.modal.entity.ShopOrderSku;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @menu ShopOrderSettlementTX
 * @date 2023/07/14 16:40
 */
@Component
public class ShopOrderSettlementTX {
    @Autowired
    private ShopOrderSkuService shopOrderSkuService;

    @Autowired
    private ShopOrderSettlementDao shopOrderSettlementDao;

    @Autowired
    private FinanceBusinessService financeBusinessService;

    @Autowired
    private H2ShopBaseService shopBaseService;

    @Resource
    private ApplicationContext applicationContext;

    //转账
    @Transactional(propagation= Propagation.REQUIRED,rollbackFor = Exception.class)
    public void handleTransferAccounts(SettlementHandleContext handle){
        H2ShopBase shop=shopBaseService.queryById(handle.getSettlementInfo().getShopId());
        BigDecimal zero=new BigDecimal("0");
        //如果有补贴，需要先将补贴金额从平台账户转入集散账户
        if (handle.getSettlementInfo().getShopSubsidyProfit().compareTo(new BigDecimal("0")) >0) {

            FinancePayDTO payDTO = new FinancePayDTO();
            payDTO.setUserId(shop.getUserId());
            payDTO.setShopId(handle.getSettlementInfo().getShopId());
            payDTO.setBusinessNo(handle.getOrderSku().getSkuOrderId());
            payDTO.setAmountIncurred(handle.getSettlementInfo().getShopSubsidyProfit());
            payDTO.setBusinessType(AccountBusinessTypeEnum.ORDER_SUBSIDY_PROFIT_FREEZE);
            financeBusinessService.pay(payDTO);
            if (!payDTO.getSuccess()){
                throw new QysException(payDTO.getMessage());
            }
        }
        //转平台
        if(handle.getSettlementInfo().getPlatformProfit()!=null&& handle.getSettlementInfo().getPlatformProfit().compareTo(zero) > 0){
            FinancePayDTO payDTO = new FinancePayDTO();
            payDTO.setUserId(shop.getUserId());
            payDTO.setShopId(handle.getOrderSku().getShopId());
            payDTO.setBusinessNo(handle.getOrderSku().getSkuOrderId());
            payDTO.setAmountIncurred(handle.getSettlementInfo().getPlatformProfit());
            payDTO.setBusinessType(AccountBusinessTypeEnum.ORDER_PROFIT_PLATFORM_GET);
            financeBusinessService.pay(payDTO);
            if (!payDTO.getSuccess()){
                throw new QysException(payDTO.getMessage());
            }
        }

        //转主播
        if (handle.getSettlementInfo().getAnchorUserId()!=null&& handle.getSettlementInfo().getAnchorProfit().compareTo(zero) > 0) {
            FinancePayDTO payDTO = new FinancePayDTO();
            payDTO.setUserId(handle.getSettlementInfo().getAnchorUserId());
            payDTO.setBusinessNo(handle.getOrderSku().getSkuOrderId());
            payDTO.setAmountIncurred(handle.getSettlementInfo().getAnchorProfit());
            payDTO.setBusinessType(AccountBusinessTypeEnum.ORDER_PROFIT_ANCHOR_GET);
            financeBusinessService.pay(payDTO);
            if (!payDTO.getSuccess()){
                throw new QysException(payDTO.getMessage());
            }
        }
        //转上级
        if(handle.getSuperiorInfos()!=null){
            for (SuperiorInfo superiorInfo : handle.getSuperiorInfos()) {
                if (superiorInfo.getProfit().compareTo(zero) == 1) {
                    FinancePayDTO payDTO = new FinancePayDTO();
                    payDTO.setUserId(Long.parseLong(superiorInfo.getUserId()));
                    payDTO.setShopId(superiorInfo.getShopId());
                    payDTO.setBusinessNo(handle.getOrderSku().getSkuOrderId());
                    payDTO.setAmountIncurred(superiorInfo.getProfit());
                    payDTO.setBusinessType(AccountBusinessTypeEnum.ORDER_PROFIT_SUPERIOR_GET);
                    financeBusinessService.pay(payDTO);
                    if (!payDTO.getSuccess()) {
                        throw new QysException(payDTO.getMessage());
                    }
                }
            }
        }
        //转店铺
        if (handle.getSettlementInfo().getShopId()!=null){
            FinancePayDTO payDTO = new FinancePayDTO();
            payDTO.setUserId(shop.getUserId());
            payDTO.setShopId(handle.getSettlementInfo().getShopId());
            payDTO.setBusinessNo(handle.getOrderSku().getSkuOrderId());
            //售价小于成本价
            if (handle.getSettlementInfo().getTotalCostAmount().compareTo(handle.getSettlementInfo().getTotalActualAmount())>0){
                payDTO.setAmountIncurred(handle.getSettlementInfo().getTotalActualAmount());
            }
            //售价大于成本价
            if (handle.getSettlementInfo().getTotalCostAmount().compareTo(handle.getSettlementInfo().getTotalActualAmount())<=0) {
                payDTO.setAmountIncurred(handle.getSettlementInfo().getTotalCostAmount());
            }
            payDTO.setBusinessType(AccountBusinessTypeEnum.ORDER_COST);
            financeBusinessService.pay(payDTO);
            if (!payDTO.getSuccess()){
                throw new QysException(payDTO.getMessage());
            }

            if (handle.getSettlementInfo().getShopProfit().compareTo(zero)>0){
                FinancePayDTO pay = new FinancePayDTO();
                payDTO.setUserId(shop.getUserId());
                pay.setShopId(handle.getSettlementInfo().getShopId());
                pay.setBusinessNo(handle.getOrderSku().getSkuOrderId());
                pay.setBusinessType(AccountBusinessTypeEnum.ORDER_PROFIT);
                pay.setAmountIncurred(handle.getSettlementInfo().getShopProfit());
                financeBusinessService.pay(pay);
                if (!pay.getSuccess()){
                    throw new QysException(pay.getMessage());
                }
            }

        }

        if (handle.getSettlementInfo().getShopSubsidyProfit().compareTo(new BigDecimal("0")) >0) {

            FinancePayDTO payDTO = new FinancePayDTO();
            payDTO.setUserId(shop.getUserId());
            payDTO.setShopId(handle.getSettlementInfo().getShopId());
            payDTO.setBusinessNo(handle.getOrderSku().getSkuOrderId());
            payDTO.setAmountIncurred(handle.getSettlementInfo().getShopSubsidyProfit());
            payDTO.setBusinessType(AccountBusinessTypeEnum.ORDER_SUBSIDY_PROFIT);
            financeBusinessService.pay(payDTO);
            if (!payDTO.getSuccess()){
                throw new QysException(payDTO.getMessage());
            }
        }
        //扣除货款
//        if (Objects.equals(Constants.YesNoInt.YES,handle.getSettlementInfo().getReturnCost())){
//            FinancePayDTO payDTO = new FinancePayDTO();
//            payDTO.setUserId(shop.getUserId());
//            payDTO.setShopId(handle.getSettlementInfo().getShopId());
//            payDTO.setBusinessNo(handle.getOrderSku().getSkuOrderId());
//            payDTO.setAmountIncurred(handle.getSettlementInfo().getTotalCostAmount());
//            payDTO.setBusinessType(AccountBusinessTypeEnum.PURCHASE);
//            financeBusinessService.pay(payDTO);
//            if (!payDTO.getSuccess()){
//                throw new QysException(payDTO.getMessage());
//            }
//        }
        //同步订单状态
        ShopOrderSku order = new ShopOrderSku();
        order.setId(handle.getOrderSku().getId());
        order.setSettlementStatus(OrderStatus.SettlementStatus.YES);
        shopOrderSkuService.updateById(order);

        handle.getSettlementInfo().setStatus(1);
        handle.getSettlementInfo().setPaymentDate(LocalDateTime.now());
        shopOrderSettlementDao.updateById(handle.getSettlementInfo());

    }

    public void saveShopOrderSettlementInfo(ShopOrderSettlementInfo settlementInfo){
        shopOrderSettlementDao.save(settlementInfo);
    }
}

